package com.vote.www.controller;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by 汤伟杰 on 2017/4/19.
 */
//@Controller
//@RequestMapping("/log")
public class LogController extends BaseController{

    @RequestMapping("open.do")
    public void open(HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws IOException {
        try {
            String tomcat_bin = System.getProperty("user.dir");//E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin
            String os = System.getProperty("os.name");
            String tomcat_log = "";
            if(os.toLowerCase().startsWith("win")){
                tomcat_log = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("\\"))+"\\logs\\catalina.out";
            }else{
                tomcat_log = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("/"))+"/logs/catalina.out";
            }
            System.out.println("log日志路径："+tomcat_log);
            StringUtils.isBlank(tomcat_log);
            Class<IOUtils> clazz = IOUtils.class;
            List<String> liststr =  org.apache.commons.io.FileUtils.readLines(new File(tomcat_log));
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            for (String s : liststr) {
                out.append("<div>"+s+"</div>");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"00","操作出错","",resp);
        }

    }
    @RequestMapping("clear.do")
    public void clear(HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model) throws IOException {
        try {
            String tomcat_bin = System.getProperty("user.dir");//E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin
            String os = System.getProperty("os.name");
            String tomcat_log = "";
            if(os.toLowerCase().startsWith("win")){
                tomcat_log = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("\\"))+"\\logs\\catalina.out";
            }else{
                tomcat_log = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("/"))+"/logs/catalina.out";
            }
            System.out.println("log日志路径："+tomcat_log);
            FileUtils.writeStringToFile(new File(tomcat_log),"");
            jsonResult(1,"","清除成功","",resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"00","操作出错","",resp);
        }

    }
}
